window.onload = function () {
    let indexs = document.querySelector('.left-nav'),
        indexP = document.querySelectorAll('.left-nav a'),
        items = document.querySelectorAll('.itemTop');
    for (let x = 0; x < indexP.length; x++) {
        indexP[x].setAttribute('index', x);
        indexP[x].onclick = function () {
            for (let i = 0; i < indexP.length; i++) {
                indexP[x].className = '';
            }
            var index = this.getAttribute('index');
            for (let j = 0; j < items.length; j++) {
                items[j].style.display = 'none';
            }
            items[index].style.display = 'block';

        }
    }
    // 用户密码重置
    document.querySelectorAll('.rest').forEach(a => {

        a.onclick = function () {
            if (!confirm('确认是否重置密码')) {
                return
            }
            let u = this.getAttribute('data-uid')
            let xhr = new XMLHttpRequest();
            xhr.open('POST', '/admin/usort', true)
            xhr.responseType = 'json'
            xhr.setRequestHeader('Content-Type', 'Application/x-www-form-urlencoded')
            xhr.send(`uid=${u}`)

            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {

                    if (xhr.response.code == 200) {
                        // alert(xhr.response.msg)
                        alert('重置密码成功，初始密码为123456')
                        location.href = '/admin/showadmin';
                    } else {

                    }
                }
            }

        }


    })

    //文章删除
    document.querySelectorAll('.delet').forEach(a => {

        a.onclick = function () {
            if (!confirm('确认是否删除')) {
                return
            }
            let u = this.getAttribute('data-tid')
            let xhr = new XMLHttpRequest();
            xhr.open('POST', '/admin/textd', true)
            xhr.responseType = 'json'
            xhr.setRequestHeader('Content-Type', 'Application/x-www-form-urlencoded')
            xhr.send(`tid=${u}`)

            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {

                    if (xhr.response.code == 200) {
                        alert("删除成功")
                        location.href = '/admin/showadmin';
                    } else {
                        alert("删除失败")

                    }
                }
            }

        }


    })
    // 评论删除
    document.querySelectorAll('.delet_com').forEach(a => {

        a.onclick = function () {
            if (!confirm('确认是否删除')) {
                return
            }
            let u = this.getAttribute('data-cid')
            let xhr = new XMLHttpRequest();
            xhr.open('POST', '/admin/comment', true)
            xhr.responseType = 'json'
            xhr.setRequestHeader('Content-Type', 'Application/x-www-form-urlencoded')
            xhr.send(`cid=${u}`)

            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {

                    if (xhr.response.code == 200) {
                        // alert(xhr.response.msg)
                        location.href = '/admin/showadmin';
                    } else {

                    }
                }
            }

        }
    })

    



}


